package com.tjkcht.midware.reader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.shhic.itsed.mws.deviceintf.AntennaProtocol;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;

import rdp_msg.Rdp.AntPara;
import rdp_msg.Rdp.ConfigPara;
import rdp_msg.Rdp.ProtocalPara;
import rdp_msg.Rdp.RdpTagReportFilterPara;
import rdp_msg.Rdp.ReadPara;
import rdp_msg.Rdp.StartTriggerPara;
import rdp_msg.Rdp.StopTriggerPara;
import rdp_msg.Rdp.rdp_request;
import rdp_msg.Rdp.rdp_request.HeadType;

/**
 * 简化RD30的消息构造.
 */
public class Rd30ParaBuilder {

	/**
	 * Gets the read request.
	 *
	 * @param cmd
	 *            the cmd
	 * @return the read request
	 */
	// 目前默认每个天线的协议和读取的标签区域都是一样的
	public static rdp_request getReadRequest(ReaderCommand cmd, Map<String, String> initParams) {
		rdp_request.Builder req = rdp_request.newBuilder();
		req.setType(HeadType.setConfig);
		ConfigPara.Builder cpb = ConfigPara.newBuilder();
		// 天线参数
		for (AntennaProtocol ap : cmd.getAntennaProtocolSet()) {
			cpb.addAntenna(getAntPara(ap.getAntennaNo(),initParams));
		}
		// 协议参数
		AntennaProtocol ap = cmd.getAntennaProtocolSet().iterator().next();
		// 0=国标 1=epc 2=行标 目前只支持epc
		for (String protocolStr : ap.getProtocols()) {
			if (protocolStr.equals(AntennaProtocol.P_EPC)) {
				cpb.addProtocal(getProtocalPara(cmd, 1,initParams));
			}
			// if(protocolStr.equals(AntennaProtocol.P_6B)){
			// cpb.addProtocal(getProtocalPara(cmd,0));
			// }
		}
		// 开始和停止的触发条件
		StartTriggerPara.Builder start = StartTriggerPara.newBuilder();
		start.setType(0);
		cpb.setStartTrigger(start);
		StopTriggerPara.Builder stop = StopTriggerPara.newBuilder();
		switch (cmd.getReaderCommandType()) {
		case SINGLE:
			stop.setType(2);
			stop.setCycle(1);
			break;
		case CYCLE:
			stop.setType(0);
			break;
		}
		cpb.setStopTrigger(stop);
		
		if(initParams.get("isFilter")!=null&& new Boolean(initParams.get("isFilter"))==true){
			RdpTagReportFilterPara.Builder filter = RdpTagReportFilterPara.newBuilder();
			filter.setEnableTagReportFilter(true);
			filter.setFilterInterval(Integer.parseInt(initParams.get("filter_time")));
			cpb.setTagReportFilter(filter);
		}
		System.out.println("initParams.get('isFilter') :"+initParams.get("isFilter"));
		System.out.println("initParams.get('filter_time') :"+initParams.get("filter_time"));
		req.setConfig(cpb);
		
		return req.build();
	}

	/**
	 * 标签的读写区域目前只考虑epc. gbmb_array[]={0,0x10,0x20,0x30};
	 * epcmb_array[]={0,1,2,3}; hbmb_array[]={0,1,2,3,4,5};
	 * 
	 * @param cmd
	 *            the cmd
	 * @return the read para list
	 */
	public static List<ReadPara> getReadParaList(ReaderCommand cmd) {
		List<ReadPara> list = new ArrayList<ReadPara>();
		int i = 1;
		for (RawTagField field : cmd.getReadTagFieldList()) {
			ReadPara.Builder rp = ReadPara.newBuilder();
			switch (field.getBank()) {
			case EPCBANK:
				rp.setMb(1);
				break;
			case TID:
				rp.setMb(2);
				break;
			case USER:
				rp.setMb(3);
				break;
			case RESERVED:
				rp.setMb(0);
				break;
			}
			rp.setOpid(i);
			i++;
			rp.setPointer(field.getOffset());
			rp.setCount(field.getLength());
			list.add(rp.build());
		}
		return list;
	}

	/**
	 * Gets the ant para.
	 *
	 * @param antid
	 *            the antid
	 * @return the ant para
	 */
	public static AntPara getAntPara(int antid, Map<String, String> initParams) {
		AntPara.Builder ap = AntPara.newBuilder();
		ap.setAntId(antid);		
		//ap.addFreq(Integer.parseInt(initParams.get("freq")));
		String[] freq = initParams.get("freq").split(",");
		System.out.println("freq is:"+freq);
		List<Integer> freqs = new ArrayList<Integer>();
		for (int i = 0;i<freq.length;i++){
			freqs.add(Integer.parseInt(freq[i]));
		}
		System.out.println("freqs is:"+freqs.toString());
		
//		freqs.add(920125);
//		freqs.add(920375);
//		freqs.add(920625);
//		freqs.add(920875);
//		freqs.add(921125);
//		freqs.add(921375);
//		freqs.add(921625);
	
		ap.addAllFreq(freqs);
		ap.setPower(Integer.parseInt(initParams.get("power")));
		return ap.build();
	}

	/**
	 * Gets the protocal para.
	 *
	 * @param cmd
	 *            the cmd
	 * @param type
	 *            the type
	 * @return the protocal para
	 */
	public static ProtocalPara.Builder getProtocalPara(ReaderCommand cmd, int type, Map<String, String> initParams) {
		ProtocalPara.Builder pp = ProtocalPara.newBuilder();
		// 0=国标 1=epc 2=行标
		pp.setProtocalType(type);		
		pp.setCodeType(Integer.parseInt(initParams.get("code_type")));
		pp.setForwardRate(Integer.parseInt(initParams.get("forward_rate")));
		pp.setReverseRate(Integer.parseInt(initParams.get("reverse_rate")));
		pp.addAllRead(getReadParaList(cmd));
		return pp;
	}

	/**
	 * Gets the read para.
	 *
	 * @param opid
	 *            the opid
	 * @param mb
	 *            the mb
	 * @param pointer
	 *            the pointer
	 * @param count
	 *            the count
	 * @return the read para
	 */
	public static ReadPara.Builder getReadPara(int opid, int mb, int pointer, int count) {
		ReadPara.Builder rp = ReadPara.newBuilder();
		rp.setOpid(opid);
		rp.setMb(mb);
		rp.setPointer(pointer);
		rp.setCount(count);
		return rp;
	}
}
